﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cvk.News;

namespace BatDongSan.Modules
{
    public partial class Uct_News : System.Web.UI.UserControl
    {
        public static int _parentId;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void LoadNav(int catId)
        {
            try
            {
                int parentId = NewsBll.claNewsType.GetParentId(catId, Utility.strConnect);
                if (parentId > 0)
                {
                    ltrnav.Text = string.Format("<div class=\"navlink\"><a href=\"/News.aspx?idt={0}\">{1}</a></div>", parentId, NewsBll.claNewsTypeDetail.GetTypeName(1, parentId, Utility.strConnect));
                    ltrnav.Text += "<span>></span>";
                    ltrnav.Text += string.Format("<div class=\"navlink\"><a href=\"/News.aspx?idt={0}\">{1}</a></div>", parentId, NewsBll.claNewsTypeDetail.GetTypeName(1, catId, Utility.strConnect));
                }
                else { ltrnav.Text = string.Format("<div class=\"navlink\"><a href=\"/News.aspx?idt={0}\">{1}</a></div>", parentId, NewsBll.claNewsTypeDetail.GetTypeName(1, catId, Utility.strConnect)); }
            }
            catch { }
        }
        public void LoadNews(int ParentId)
        {
            try
            {
                LoadNav(ParentId);
                _parentId = ParentId;
                //lblTitle.Text = string.Format("Các tin {0} đã đăng", NewsBll.claNewsTypeDetail.GetTypeName(1, ParentId, Utility.strConnect));
                //dtlNews.DataSource = NewsBll.claNews.GetListNewsByParent(Global.idCulture(), ParentId, 0, 5, true, Global.strConnect());
                List<VG_New> list = NewsBll.claNews.GetListNewsByParent(1, ParentId, (pager1.CurrentIndex - 1) * pager1.PageSize, pager1.PageSize, true, Utility.strConnect);
                dtlNews.DataSource = list;
                dtlNews.DataBind();


                Int32 _totalRecords = NewsBll.claNews.GetListNewsByParentCount(1, ParentId, true, Utility.strConnect);
                pager1.ItemCount = _totalRecords;

                if (_totalRecords > pager1.PageSize)
                    pager1.Visible = true;
                else
                    pager1.Visible = false;
            }
            catch { }
        }
        protected void dtlNews_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    VG_New info = (VG_New)e.Item.DataItem;

                    Literal ltrImg = (Literal)e.Item.FindControl("ltrImg");
                    ltrImg.Text = string.Format("<img src=\"/Uploads/News/{0}\" width=\"120px\" height=\"82px\" />", info.ImageThumb);

                    HyperLink hplSubject = (HyperLink)e.Item.FindControl("hplSubject");
                    hplSubject.Text = hplSubject.ToolTip = info.Subject;
                    hplSubject.NavigateUrl = string.Format("/NewsDetail.aspx?idt={0}&id={1}", info.NewsTypeId, info.NewsId);
                    //hplSubject.NavigateUrl = string.Format("http://{0}/{1}/{2}/{3}.htm", Request.Url.Authority, NewsBll.claNewsTypeDetail.GetUrlFriendly(1, info.NewsTypeId, Utility.strConnect), info.NewsId, info.SeoUrl);


                    //if ((bool)info.IsHoted)
                    //{
                    //    Literal ltrNew = (Literal)e.Item.FindControl("ltrNew");
                    //    ltrNew.Text = "&nbsp;<img src=\"/imgs/new.gif\" border=\"0\">";
                    //    hplSubject.Font.Bold = true;
                    //}

                    //Label lblDate = (Label)e.Item.FindControl("lblDate");
                    //lblDate.Text = Convert.ToDateTime(info.PostDate).ToString("dd/MM/yyyy");

                    Label lblNote = (Label)e.Item.FindControl("lblNote");
                    lblNote.Text = info.Note;
                }
            }
            catch { }
        }
        protected void mktpage_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
                pager1.CurrentIndex = currnetPageIndx;
                LoadNews(_parentId);
            }
            catch { }
        }
    }
}